<template>
	<view class="content">
		<image class="logo" src="../../static/logo.png"></image>
		<view>
			<text class="title">{{title}}</text>
		</view>
		<view class="uni-list">
			<view class="uni-list-cell" v-for="(item,index) in btnTitles" :key="index">
				<button class="uni-list-cell-navigate uni-navigate-right" @click="btnclick(index)">
					{{item}}
				</button>
			</view>
		</view>
		<text>{{result}}</text>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: '阿里百川UniApp插件',
				btnTitles: ['百川初始化', '授权并登录', '用户是否已登录', '退出登录', '跳转购物车', '跳转到我的订单',商品详情],
				result: '',
			}
		},
		onLoad() {
		},
		methods: {
			btnclick: function(e) {
				const bcPlugin = uni.requireNativePlugin('dahui-baichuan');
				switch (e) {
					case 0:
						if (plus.device.vendor == 'Apple') {
							bcPlugin.BCSDKInitWithAppkey('25254125', result => {
								uni.showModal({
									title: '百川初始化',
									content: 'code:' + result['code'] + '\nmessage:' + result['message']
								})
							});
						}
						this.result = '点击了：' + this.btnTitles[e];
						break;
					case 1:
						if (plus.device.vendor == 'Apple') {
							bcPlugin.BCAuth(result => {
								uni.showModal({
									title: '授权登录',
									content: 'code:' + result['code'] + '\nmessage:' + result['message']
								})
							});
						} else {
							bcPlugin.login(result => {
								if (result.type == "success") {
									uni.showToast({
										title: '授权成功'
									});
								} else {
									//授权失败，包括用户取消登录	
									uni.showToast({
										title: '授权失败,如有异常，请联系客服。'
									});
								}
							});
						}
						this.result = '点击了：' + this.btnTitles[e];
						break;
					case 2:
						if (plus.device.vendor == 'Apple') {
							bcPlugin.BCIsUserLogin(result => {
								uni.showModal({
									title: '用户是否登录',
									content: 'code:' + result['code'] + '\nmessage:' + result['message']
								})
							});
						} else {
							bcPlugin.checkSession(reg => {
								if (reg) {
									//reg 存在 获取seesion
										uni.showModal({
										title: 'session',
										content: 'reg:'+reg
									})
								} else {
									//reg过期 获取session
									bcPlugin.getSession(result => {});
								}
							});
						}
						this.result = '点击了：' + this.btnTitles[e];
						break;
					case 3:
						if (plus.device.vendor == 'Apple') {
							bcPlugin.BCUserLogout(result => {
								uni.showModal({
									title: '退出登录',
									content: 'code:' + result['code'] + '\nmessage:' + result['message']
								})
							});
						} else {
							bcPlugin.logout(result => {
								if (result.type == "success") {

								} else {
									//退出失败
									console.log("code:" + result.code + " - " + "msg:" + result.msg);
								}
							});
						}
						this.result = '点击了：' + this.btnTitles[e];
						break;
					case 4:
						//淘宝购物车
						if (plus.device.vendor == 'Apple') {
							bcPlugin.BCJumpMyCart(result => {
							});
						} else {
							bcPlugin.OpenMyCart(result => {
							});
						}
						this.result = '点击了：' + this.btnTitles[e];
						break;
					case 5:
						//我的订单
						if (plus.device.vendor == 'Apple') {
							bcPlugin.BCJumpMyOrder(result => {
							});
						} else {
							bcPlugin.OpenMyOrders({
								status: 0,
								type: true
							}, result => {
							});
						}
						this.result = '点击了：' + this.btnTitles[e];
						break;
					case 6:
						//商品详情（指定URL跳转淘宝）
						if (plus.device.vendor == 'Apple') {
						//ios 跳转详情页
						bcPlugin.BCGetCoupons(
							_self.success,
							result => {});
						} else {
						//adnroid 跳转详情页
						bcPlugin.url({
							url: _self.success
						}, result => {

						});
						}
					default:
						break;
				}
			}
		}
	}
</script>

<style>
	.content {
		text-align: center;
		height: 400upx;
	}

	.logo {
		height: 200upx;
		width: 200upx;
		margin-top: 20upx;
	}

	.title {
		font-size: 36upx;
		color: #8f8f94;
	}
</style>
